﻿
#include "../ToolBarProperty.h"
#include "../MeasureView.h"
#include <QLabel>
#include <QSpinBox>
#include "../ColorComboBox.h"

namespace Qly {


ToolBarProperty::ToolBarProperty(const QString &title, QWidget *parent)
    :QToolBar(title, parent)
{
    setIconSize(QSize(32, 32));
    initContent();
    pMeasureView = nullptr;
}

ToolBarProperty::ToolBarProperty(QWidget *parent)
    :QToolBar(parent)
{
    setIconSize(QSize(32, 32));
    initContent();
    pMeasureView = nullptr;
}

void ToolBarProperty::loadSettings(QSettings &settings)
{
    int linewidth = settings.value("Display/LineWidth", 2).toInt();
    int textsize = settings.value("Display/TextSize", 15).toInt();
    int colorindex = settings.value("Display/ColorIndex", 7).toInt();
    spinBoxLineWidth->setValue(linewidth);
    spinBoxTextSize->setValue(textsize);
    colorBox->setCurrentIndex(colorindex);
}

void ToolBarProperty::writeSettings(QSettings &settings)
{
    int linewidth = spinBoxLineWidth->value();
    int textsize = spinBoxTextSize->value();
    int colorindex = colorBox->currentIndex();

    settings.setValue("Display/LineWidth", linewidth);
    settings.setValue("Display/TextSize", textsize);
    settings.setValue("Display/ColorIndex", colorindex);
}

void ToolBarProperty::on_spinBoxLineWidthValueChanged(int v)
{
    if(pMeasureView)
    {
        MeasureDataSet &dataset = pMeasureView->dataSet();
        dataset.setLineWidth(v);
    }

}

void ToolBarProperty::on_spinBoxTextSizeValueChanged(int v)
{
    if(pMeasureView)
    {
        MeasureDataSet &dataset = pMeasureView->dataSet();
        dataset.setFontSize(v);
    }
}

void ToolBarProperty::on_colorBoxColorChanged(Qt::GlobalColor c)
{
    if(pMeasureView)
    {
        MeasureDataSet &dataset = pMeasureView->dataSet();
        dataset.setColor(QColor(c));
    }
}

void ToolBarProperty::on_actionUndoTriggered()
{
    if(pMeasureView)
    {
        MeasureDataSet &dataset = pMeasureView->dataSet();
        dataset.undo();
    }
}

void ToolBarProperty::on_actionDeleteAllTriggered()
{
    if(pMeasureView)
    {
        MeasureDataSet &dataset = pMeasureView->dataSet();
        dataset.clearAll();
    }
}

void ToolBarProperty::attach(MeasureView *widget)
{
    pMeasureView = widget;

    if(pMeasureView)
    {
        spinBoxTextSize->setValue(pMeasureView->dataSet().textSize());
        spinBoxLineWidth->setValue(pMeasureView->dataSet().lineWidth());
        colorBox->setColor(pMeasureView->dataSet().color());
    }


//    connect(centerWidget, SIGNAL(lineWidthChanged(int)), spinBoxLineWidth, SLOT(setValue(int)));
//    connect(centerWidget, SIGNAL(textSizeChanged(int)), spinBoxTextSize, SLOT(setValue(int)));
}

void ToolBarProperty::initContent()
{
    setWindowTitle("Property Setup Toolbar");
    setObjectName(QStringLiteral("ToolBarProperty"));

    QLabel *label = new QLabel(tr("Line Width:"), this);
    addWidget(label);
    spinBoxLineWidth = new QSpinBox;
    spinBoxLineWidth->setRange(1, 20);
    spinBoxLineWidth->setValue(2);
    spinBoxLineWidth->setSingleStep(1);
    spinBoxLineWidth->setFixedWidth(50);
    addWidget(spinBoxLineWidth);
    addSeparator();

    label = new QLabel(tr("Font Size:"), this);
    addWidget(label);
    spinBoxTextSize = new QSpinBox;
    spinBoxTextSize->setRange(6, 50);
    spinBoxTextSize->setValue(12);
    spinBoxTextSize->setSingleStep(1);
    spinBoxTextSize->setFixedWidth(50);
    addWidget(spinBoxTextSize);
    addSeparator();

    colorBox = new ColorComboBox();
    addWidget(colorBox);
    colorBox->setCurrentIndex(1);
    addSeparator();

    actionUndo = new QAction(tr("undo"), this);
    actionUndo->setObjectName(QStringLiteral("actionUndo"));
    QIcon icon7;
    icon7.addFile(":/Measurement/undo.png", QSize(), QIcon::Normal, QIcon::Off);
    actionUndo->setIcon(icon7);
    actionDeleteAll = new QAction(tr("delete all"), this);
    actionDeleteAll->setObjectName(QStringLiteral("actionDeleteAll"));
    QIcon icon8;
    icon8.addFile(":/Measurement/delete-all.png", QSize(), QIcon::Normal, QIcon::Off);
    actionDeleteAll->setIcon(icon8);

    addAction(actionUndo);
    addAction(actionDeleteAll);

    connect(spinBoxLineWidth, qOverload<int>(&QSpinBox::valueChanged), this, &ToolBarProperty::on_spinBoxLineWidthValueChanged);
    connect(spinBoxTextSize, qOverload<int>(&QSpinBox::valueChanged), this, &ToolBarProperty::on_spinBoxTextSizeValueChanged);
    connect(colorBox, &ColorComboBox::colorChanged, this, &ToolBarProperty::on_colorBoxColorChanged);

    connect(actionUndo, &QAction::triggered, this, &ToolBarProperty::on_actionUndoTriggered);
    connect(actionDeleteAll, &QAction::triggered, this, &ToolBarProperty::on_actionDeleteAllTriggered);
}
} //namespace Qly
